/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dressingsocial.controller;

import com.dressingsocial.model.Dressing;
import com.dressingsocial.model.Opinion;
import com.dressingsocial.model.User;
import com.dressingsocial.service.DressingService;
import com.dressingsocial.service.OpinionService;
import com.dressingsocial.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author hayate
 */
@Controller
public class OpinionController {

    @Autowired
    DressingService dressingService;

    @Autowired
    OpinionService opinionService;

    @Autowired
    UserService userService;
    
    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String createOpinionPage(Model model) {
        Opinion opinion = new Opinion();
        model.addAttribute("opinion", opinion);
        return "opinions/new";
    }

    @RequestMapping(value = "/dressings/{id}/opinions", method = RequestMethod.POST)
    public String createOpinion(
            @PathVariable Long id,
            Opinion opinion) {
        Dressing dressing = dressingService.findById(id);
        opinion.setDressing(dressing);
        
        String email = (String)SecurityUtils.getSubject().getPrincipal();
        User user = userService.findByEmail(email);
        opinion.setUser(user);
        
        opinionService.create(opinion);

        return "redirect:/dressings/" + id + "/show";
    }

    //@RequestMapping(method = RequestMethod.GET)
    public String getOpinionsOnDressing() {
        
        return "dressing.show";
    }
}
